#include "process.h"
#include <unistd.h>
#include <time.h>
#include <stdlib.h>

double gtotal = 1024.0;
double speed = 1.0;

//函数指针类型
typedef void (*callback_t)(double, double);

double SpeedFloat(double start, double range){
  int int_range = (int)range;
  return start + rand() % int_range + (range - int_range);
}

//cb:回调函数
void DownLoad(int total, callback_t cb){
  double curr = 0.0;
  while(curr <= total){
    cb(total,curr);    //更新进度
    curr += SpeedFloat(speed,20.3);     //模拟下载行为
    if(curr > total){
      curr = total;
      cb(total,curr);    //更新进度
      break;
    }
    usleep(30000);
  }
}
int main(){
  printf("DownLoad : 100MB\n");
  
  DownLoad(100,FlushProcess);
  printf("DownLoad: 1000MB\n");
  DownLoad(1000,FlushProcess);
  return 0;
}
